package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.ExistsListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Existsiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.ExistsService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;



public class ExistsModel implements Existsiface{
    private ExistsService existsService;
    private Retrofit retrofit;
    private static final  String BASE_URL=
            "http://amicool.neusoft.edu.cn/";
    public ExistsModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        ScalarsConverterFactory.create())
                .build();

    }

    @Override
    public void exists(String mod, int idolid, String sessionID, final ExistsListener existsListener) {
        existsService=retrofit.create(ExistsService.class);
        Call<String>call=existsService.exists(mod,idolid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    existsListener.onexistsResponse(response.body());
                else
                    existsListener.onexistsFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                existsListener.onexistsFail("error");
            }
        });
    }

    @Override
    public void unexists(String mod, int idolid, String sessionID, final ExistsListener existsListener) {
        existsService=retrofit.create(ExistsService.class);
        Call<String>call=existsService.unexists(mod,idolid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    existsListener.oncancelResponse(response.body());
                else
                    existsListener.oncancelFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                existsListener.oncancelFail("error");
            }
        });
    }

    @Override
    public void judge(String mod, int idolid, String sessionID, final ExistsListener existsListener) {
        existsService=retrofit.create(ExistsService.class);
        Call<String>call=existsService.judge(mod,idolid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    existsListener.onjudgeResponse(response.body());
                else
                    existsListener.onjudgeFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                existsListener.onjudgeFail("error");
            }
        });
    }


    }


